/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PengajarDialog.java
 *
 * Created on Aug 26, 2014, 7:39:37 PM
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.KelasDAO;
import com.ivanbiz.dao.impl.KelasDAOImpl;
import com.ivanbiz.model.AksesMatrix;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.service.GlobalSession;
import com.ivanbiz.service.MenuAksesConstant;
import com.ivanbiz.service.ServiceHelper;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Shbt Peterpan
 */
public class KelasDialog extends JDialog {

    Kelas kelas;
    List<Kelas> listKelas;
    KelasDAO kelasDAO = new KelasDAOImpl();
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    String lulus;

    public KelasDialog() {
        initComponents();
        renderButtonAkses(GlobalSession.getListAksesMatrix());
        refresh();
        buttonDetail.setVisible(false);
        buttonTutup.setVisible(false);
    }

    public KelasDialog(String lulusOrDaftarKelas) {
        initComponents();
        refresh();
        this.lulus = lulusOrDaftarKelas;
        renderButtonAkses(GlobalSession.getListAksesMatrix());
        if (lulusOrDaftarKelas.contentEquals("kelulusan")) {
            buttonTambah.setVisible(false);
            buttonUbah.setVisible(false);
            buttonHapus.setVisible(false);
            buttonTutup.setVisible(false);
        } else {
            buttonTambah.setVisible(false);
            buttonUbah.setVisible(false);
            buttonHapus.setVisible(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableKelas = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        buttonTambah = new javax.swing.JButton();
        buttonUbah = new javax.swing.JButton();
        buttonHapus = new javax.swing.JButton();
        buttonDetail = new javax.swing.JButton();
        buttonTutup = new javax.swing.JButton();

        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Kelas");

        tableKelas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableKelas);

        buttonTambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/tambah.jpg"))); // NOI18N
        buttonTambah.setText("Tambah Kelas Baru");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });
        jPanel2.add(buttonTambah);

        buttonUbah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/ubah.jpg"))); // NOI18N
        buttonUbah.setText("Ubah Kelas Terseleksi");
        buttonUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUbahActionPerformed(evt);
            }
        });
        jPanel2.add(buttonUbah);

        buttonHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/hapus.jpg"))); // NOI18N
        buttonHapus.setText("Hapus Kelas Terseleksi");
        buttonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHapusActionPerformed(evt);
            }
        });
        jPanel2.add(buttonHapus);

        buttonDetail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/daftar_kelas.jpg"))); // NOI18N
        buttonDetail.setText("Daftar Kelas Terseleksi");
        buttonDetail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDetailActionPerformed(evt);
            }
        });
        jPanel2.add(buttonDetail);

        buttonTutup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/tutup_kelas.jpg"))); // NOI18N
        buttonTutup.setText("Tutup Kelas Terseleksi");
        buttonTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTutupActionPerformed(evt);
            }
        });
        jPanel2.add(buttonTutup);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 819, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-808)/2, (screenSize.height-627)/2, 808, 627);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        new KelasUpdateDialog(null, true).setVisible(true);
        refresh();
    }//GEN-LAST:event_buttonTambahActionPerformed

    private void buttonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHapusActionPerformed
        if (tableKelas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dihapus", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                kelas = listKelas.get(tableKelas.getSelectedRow());
                kelasDAO.delete(kelas);
                refresh();
            } catch (Exception ex) {
                Logger.getLogger(KelasDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonHapusActionPerformed

    private void buttonDetailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDetailActionPerformed
        if (tableKelas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih daftar Kelas", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            kelas = listKelas.get(tableKelas.getSelectedRow());
            if (lulus.contentEquals("kelulusan")) {
                new DaftarKelasDialog(kelas, lulus).setVisible(true);
            } else {
                new DaftarKelasDialog(kelas).setVisible(true);
            }
        }
    }//GEN-LAST:event_buttonDetailActionPerformed

    private void buttonTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTutupActionPerformed
        if (tableKelas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan ditutup", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                int response = JOptionPane.showConfirmDialog(this, "Apakah Kelas ingin ditutup ?", "Konfirmasi", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (response == JOptionPane.YES_OPTION) {
                    kelas = listKelas.get(tableKelas.getSelectedRow());
                    kelas.setStatusKelas("Y");
                    kelasDAO.saveOrUpdate(kelas);
                    refresh();
                }
            } catch (Exception ex) {
                Logger.getLogger(KelasDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonTutupActionPerformed

    private void buttonUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUbahActionPerformed
        if (tableKelas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan diubah", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            kelas = listKelas.get(tableKelas.getSelectedRow());
            new KelasUpdateDialog(null, true, kelas).setVisible(true);
            refresh();
        }
    }//GEN-LAST:event_buttonUbahActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonDetail;
    private javax.swing.JButton buttonHapus;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JButton buttonTutup;
    private javax.swing.JButton buttonUbah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableKelas;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
        try {
            listKelas = kelasDAO.getAll(Kelas.class);
            updateTableKelas();
        } catch (Exception ex) {
            Logger.getLogger(KelasDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void renderButtonAkses(List<AksesMatrix> listAksesMatrix) {
        buttonTambah.setEnabled(MenuAksesConstant.validate(MenuAksesConstant.TAMBAH_KELAS, listAksesMatrix));
        buttonUbah.setEnabled(MenuAksesConstant.validate(MenuAksesConstant.UBAH_KELAS, listAksesMatrix));
        buttonHapus.setEnabled(MenuAksesConstant.validate(MenuAksesConstant.HAPUS_KELAS, listAksesMatrix));
        buttonTutup.setEnabled(MenuAksesConstant.validate(MenuAksesConstant.TUTUP_KELAS, listAksesMatrix));
    }

    private void updateTableKelas() {
        String[] judul = {"No", "No Kelas", "Deskripsi", "Pengajar", "Tanggal", "Tanggal 2", "Tempat", "Alamat"};
        Object[][] isi = new Object[listKelas.size()][8];
        int x = 0;
        int no = 0;
        for (Kelas kelass : listKelas) {
            no += 1;
            isi[x][0] = no;
            isi[x][1] = kelass.getTransactionReference();
            isi[x][2] = kelass.getDeskripsi();
            isi[x][3] = "(" + kelass.getPengajar().getNIP() + ") - " + kelass.getPengajar().getNama();
            isi[x][4] = sdf.format(kelass.getTanggalKelas());
            isi[x][5] = kelass.getTanggalKelas2()==null?"":sdf.format(kelass.getTanggalKelas2());
            isi[x][6] = kelass.getTempatKelas();
            isi[x][7] = kelass.getAlamatKelas();
            x++;
        }
        tableKelas.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        new ServiceHelper().setAutoRize(isi, judul, tableKelas);
    }
}
